import scrapy
import json


class SpiderPostSpider(scrapy.Spider):
    name = "spider_post"
    allowed_domains = ["baidu.com"]
    start_urls = ["https://fanyi.baidu.com"]

    def start_requests(self):
        url = "https://fanyi.baidu.com/v2transapi?from=zh&to=en"
        data = {
            "from": "zh",
            "to": "en",
            "query": "你好",
            "simple_means_flag": "3",
            "sign": "232427.485594",
            "token": "deae1aa1fdbabdcb505c1cfc0f88c51d",
            "domain": "common",
            "ts": "1696486771682"
        }
        yield scrapy.FormRequest(url=url, formdata=data, callback=self.parse)

    def parse(self, response, **kwargs):
        dict_data = json.loads(response.text)
        print(dict_data['trans_result']['data'][0]['dst'])
        print(dict_data['trans_result']['data'][0]['src'])
